package org.marsdonne.auth.messenger;

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.ConnectionFactory;

@Configuration
@ConditionalOnProperty(prefix = "auth", name = "artemis.broker-url")
public class ArtemisConfigurer {
    @Value("${auth.artemis.broker-url:tcp://localhost:61616}")
    private String brokerUrl;
    @Value("${auth.artemis.username:admin}")
    private String username;
    @Value("${auth.artemis.password:admin}")
    private String password;

    @Bean
    public ConnectionFactory connectionFactory() {
        return new ActiveMQConnectionFactory(brokerUrl, username, password);
    }

    @Bean
    public JmsListenerContainerFactory authListenerFactory() {
        DefaultJmsListenerContainerFactory containerFactory = new DefaultJmsListenerContainerFactory();
        containerFactory.setConnectionFactory(connectionFactory());
        return containerFactory;
    }
}